var app = getApp();
Page({
    data:{
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        page : {
            isend : false,
            doing : false,
            doo : 'user_v1',
            pdata : {
                op : 'address',
                page : 1,
                sid : 0,
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        region : [],
        from : '',
    },
    onLoad : function(options){
        var self = this;
        self.setData({
            from : options.from ? options.from : 0,
        });
    },    
    onShow : function(options){
        var self = this;
        app.com.auth(function(user){
            self.addlist();
        });
        
        wx.setNavigationBarTitle({
          title:  '我的地址',
        });
        wx.authorize({
            scope: "scope.address",
            success: function() {
                self.setData({
                    addressAuth: true
                });
            },
            fail: function() {}
        });
    },
    addlist : function(){
        var self = this;
        self.setData({
            page : {
                isend : false,
                doing : false,
                doo : 'user_v1',
                pdata : {
                    op : 'address',
                    page : 1,
                    sid : 0,
                },
                waitf : 0,
                nodataf : 0,
            }
        });
        app.com.getPage(self,self.data.page,0,false,function(res){
            self.setData({
               datalist : res.data.data.list,
               inited : true,
               params : res.data.data.params,
            });
        },function(res){
            app.com.alert(res.data.message);
        });
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: '我的地址',
            path: '',
        }
    },
    checkAddressAuth: function() {
        var self = this;
        wx.authorize({
            scope: "scope.address",
            success: function() {
                self.setData({
                    addressAuth: true
                });
                self.addWechat();
            },
            fail: function() {}
        });
    },
    addWechat: function(a) {
        var self = this;
        wx.chooseAddress({
            success: function(a) {
                var postdata = {
                    op : 'addaddress',
                    name: a.userName,
                    tel: a.telNumber,
                    street: a.detailInfo,
                    region: a.provinceName + "," + a.cityName + "," + a.countyName,
                };
                app.com.http('user_v1','POST',postdata,0,true,'','',function(res){
                    if (!res.data.errno) {
                        self.addlist();
                        app.com.toast(res.data.message);
                    }else{
                        app.com.alert(res.data.message);
                    }
                });
            }
        })
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    },
    showadd : function(){
        this.setData({
            actid : 0,
            showadd : !this.data.showadd,
        });
    },
    bindRegionChange : function(e){
        this.setData({
            region : e.detail.value,
        });
    },
    bindinput : function( e ){
        var type = e.currentTarget.dataset.type;
        var value = e.detail.value;

        if( type == 1 ) var aaa = 'name';
        if( type == 2 ) var aaa = 'tel';
        if( type == 3 ) var aaa = 'street';

        this.setData({
            [aaa] : value,
        });
    },
    sub : function(){
        var self = this;
        var postdata = {
            op : 'addaddress',
            actid : self.data.actid,
            name : self.data.name,
            tel : self.data.tel,
            region : self.data.region,
            street : self.data.street,
        };
        if( !app.com.verify('mobile',postdata.tel) ){
           app.com.alert('手机号码格式不对');return false;
        }
        app.com.http('user_v1','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno) {
                self.setData({
                    actid : 0,
                    name : '',
                    tel : '',
                    region : [],
                    street : '',
                    showadd : false,
                });
                self.addlist();
                app.com.toast(res.data.message);
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    edit : function(e){
        var self = this;
        var id = e.currentTarget.dataset.id;
        for (var i = 0; i < self.data.datalist.length; i++) {
            if( self.data.datalist[i].id == id ) {
                if( self.data.datalist[i].params.region )  var region = self.data.datalist[i].params.region.split(",");

                self.setData({
                    actid : id,
                    name : self.data.datalist[i].params.name,
                    tel : self.data.datalist[i].params.tel,
                    region : region,
                    street : self.data.datalist[i].params.street,
                    showadd : true,
                });
            }
        }
    },
    del : function(e){
        var self = this;
        var id = e.currentTarget.dataset.id;
        app.com.http('user_v1','POST',{id:id,op:'deleteadd'},0,true,'','',function(res){
            if (!res.data.errno) {
                for (var i = 0; i < self.data.datalist.length; i++) {
                    if( self.data.datalist[i].id == id ) {
                        self.data.datalist.splice(i,1);
                    }
                }
                self.setData({
                    'datalist' : self.data.datalist
                });
                wx.showToast({
                  title: '已删除',
                  icon: 'success',
                  duration: 1000
                });
            }
        })
    },
    toact : function(e){
        var self = this;
        var id = e.currentTarget.dataset.id;
        app.com.http('user_v1','POST',{id:id,op:'toactadd'},0,true,'','',function(res){
            if (!res.data.errno) {
                wx.showToast({
                  title: '已保存',
                  icon: 'success',
                  duration: 1000
                });
                self.addlist();
            }
        })
    },
    wxadd : function(){
        var self = this;
        wx.chooseAddress({
            success: function (res) {
                self.setData({
                    actid : 0,
                    name : res.userName,
                    tel : res.telNumber,
                    region : res.provinceName+','+res.cityName+','+res.countyName,
                    street : res.detailInfo,
                    showadd : false,
                });
                self.sub();
            }
        })
    },
    useadd : function(e){
        var self = this;

        if(self.data.from != 'choose'){
            return false;
        }

        var id = e.currentTarget.dataset.id;
        for (var i = 0; i < self.data.datalist.length; i++) {
            if( self.data.datalist[i].id == id ) {
                var pages = getCurrentPages();
                var prevPage = pages[pages.length - 2];  //上一个页面
                
                if(typeof prevPage.setaddress == 'function'){
                    prevPage.setaddress(self.data.datalist[i]);
                }else{
                    prevPage.setData({
                        address: self.data.datalist[i]
                    });
                }
                wx.navigateBack();
            }
        }
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },

})